/**
 *  Java Api For Ecco Generated Applications 
 *  
 *  Copyright (C) 2000  by PDTec GmbH 
 *
 * @author  Michael Weirich
 */

package pdtec.ecco.meta;

/**
 * The class Attribute enables access to the definition of the  Express entity attributes. 
 * @see  pdtec.ecco.inst.InstApplication
 * @see  pdtec.ecco.meta.Application
 */
public class Attribute {

    /**
     * Id of the the related application instance (either Application or InstApplication). Each application instance
     * has a set of Schemas. This relationship is expressed by this reference id.
     *
     * @see pdtec.ecco.meta.Application#getId
     */
    int appId;
    
    /**
     * This id actually represents a type object pointer of the entity.
     */
    long entityId;
   
    /**
     * The type of the attribute.
     */
    long typeId;
    
    /**
     * Index of the attribute within the entity it is defined in.
     */
    int index;
    
    /**
     * The name of the attribute.
     */
    String name;
    
    /*
     * Indicates wether attribute is optional.
     */
    //private boolean isOptional;
    
    /**
     * The standard constructor is protected since instance creation is done
     * by the static {@link #get(int,long,int,long) get(...)} method.
     */
    protected Attribute() {}
    
    /** Protected Constructor. It is protected since instantiation is done
     * by the static {@link #get(int,long,int,long) get(...)} method.
     * The constructor requires a valid application id, the entity id it belongs to, its
     * index ( attributes are enumerated within an entity ) and its type. 
     * @see Attribute#get(int,long,int,long)
     */
    protected Attribute( int appId,long entityId, int index,long typeId ) {
	this.appId = appId;
	this.entityId = entityId;
	this.index = index;
	this.typeId = typeId;
    } // Attribute
    
    /**
     * Returns instances of class Attribute in the specified application context.
     * @exception EccoException in case an error occurs
     */
    static public native synchronized Attribute get(int appId,
						    long entityId, int index,long typeId) throws EccoException;
    
    /**
     * Protected internal delete operation. Prepares instance before the garbage collector may finalize it.  
     * @exception EccoException in case an error occurs
     */      
    protected native synchronized void delete() throws EccoException;
    
    /**
     * Returns the application id of the attribute instance.
     * @return the appId of the attribute
     */
    public int getAppId() {
	return(appId);
    } // getAppId
       
    /**
     * Returns the entity id of the Attribute instance.
     * @return the entityId of the attribute
     */
    public long getEntityId() {
	return(entityId);
    } // getEntityId

    /**
     * Returns the type of the attribute.
     * @return the type of the attribute
     * @exception EccoException in case an error occurs
     */
    public  Type getType() throws EccoException {
	Type type = null;
	
	type = Type.get(appId,typeId);
	return(type);
    } // getType
    
    /** Returns the index of the attribute. 
     * @return the index of the attribute 
     */
    public int getIndex() {
	return index;
    } // getIndex
    
    /**
     * Returns the name of the attribute.
     * @return the name of the attribute.
     */
    public String getName() {
	return(name);
    } // getName

    /**
     * Retruns true if the attribute is optional.
     * @returns the optional flag.
     */
    public native synchronized boolean isOptional() throws EccoException;
    
    /**
     * Finalizes the attribute instance.
     */
    protected void finalize() {
	try {
	    if( appId != -1)
		delete();
	} catch (EccoException e) {
            System.out.println(e.toString());
	} // try
    } // finalize
    
} // class Attribute






